<?php
/**
 * Created by PhpStorm.
 * User: DaibiDaddy
 * Date: 2016/5/24
 * Time: 17:14
 */

namespace App\Base;

use App\Entity\Project;
use App\Model\LazyCommon\AdminLogModel;
use App\Model\LazyCommon\CCM;
use App\Model\ProjectModel;
use Symfony\Component\HttpFoundation\Request;

class BaseController4Api extends BaseController {


    public function createToken($vi,$time,$project)
    {
        if ($time < time() - 300 || $time > time() + 500) {
//            die("time out");
        }
        return sha1($vi . $time . $project . $time . $vi );
    }

    public function getProjectId()
    {
        $request = Request::createFromGlobals();
        $project = $request->headers->get("project");
        $token = $request->headers->get("token");
        $time = $request->headers->get("t");//时间戳；
        $ProjectModel  = new ProjectModel($this->getDoctrine());
        $Project = $ProjectModel->getById($project);
        if ($Project) {
            $vi = $Project->getVi();
            $myToken = $this->createToken($vi,$time,$project);
            if (strtolower($myToken) == strtolower($token)) {
                return $project;
            }
        }
        die("access denied");

    }
}
